home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1996 May: Tool Chest / Developer CD Series May 1996 (Tool Chest) (Apple Computer) (1996).iso / Sample Code / Snippets / Testing & Debugging / Audit / Src / DisplayAuditMain.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-07-21  |  12.0 KB  |  481 lines  |  [TEXT/KAHL]

  1. /*                                    DisplayAudit.c                                */
  2. /*
  3.  * DisplayAudit.c
  4.  * Copyright © 1992-93, Apple Computer Inc. All Rights Reserved.
  5.  * Programmed by Martin Minow,
  6.  *    Internet:    minow@apple.com
  7.  *    AppleLink:    MINOW
  8.  *
  9.  * Edit History
  10.  *    93.01.09 MM        First public release
  11.  *    93.01.14 MM        Removed Think C stdio dependencies. Added Standard File dialog
  12.  *                    with additional dialog items to allow defining the audit
  13.  *                    selector.
  14.  *    93.01.19 MM        MPW \r is the wrong character. Force <return> on both MPW and
  15.  *                    Think, because this is what the Mac file system requires for
  16.  *                    end of line. (Change is in DisplayAuditFile.c). #if'ed out
  17.  *                    some test calls. Also added error status logging for file I/O
  18.  *                    errors.
  19.  *    93.01.22 MM        Added a preference file that remembers the last audit selector
  20.  *                    parameter and window sizes. Also added error alerts. Moved
  21.  *                    global parameters to DisplayAudit.h.
  22.  *    93.07.09 MM        Reformatted text. Many other changes:
  23.  *                    -- Removed "SimpleTextWindow" -- it was far too simple,
  24.  *                    replacing it with the Log Manager from DTS Catalog Peek.
  25.  *                    -- Added "number of lines to save" to the file dialog (and
  26.  *                    wherever else necessary). Also added number of audit records
  27.  *                    to create.
  28.  *                    -- Revised overall structure so that we can (eventually)
  29.  *                    support multiple Audit records.
  30.  *                    -- Removed fixed "number of audit cycles," replacing it with
  31.  *                    the number of audit records we've created.
  32.  *                    -- Write parameter record whenever it changes. This includes
  33.  *                    changes in the window shape. Note: the parameter record will
  34.  *                    reflect the last window you created. If you create two, only
  35.  *                    the second will be in the parameter record. Actually, the
  36.  *                    "mainline" has been extensively rewritten.
  37.  *
  38.  * This stand-alone application displays the data from a AuditRecord, writing it
  39.  * to a file or the screen. It is not very intelligent.
  40.  */
  41. #define EXTERN        /* */
  42. #include "DisplayAudit.h"
  43.  
  44. void                                ProcessAudit(void);
  45. void                                DisplayLogEntry(void);
  46. void                                DumpAuditEntry(
  47.         register AuditPtr                auditPtr,
  48.         register AuditEntryPtr            entryPtr
  49.     );
  50.  
  51. void                                InitMacintosh(void);
  52. void                                InitApplication(void);
  53. void                                EventLoop(void);
  54. void                                ProcessAudit(void);
  55. void                                DoMouseEvent(void);
  56. void                                DoCommand(
  57.         WindowPtr                        theWindow,
  58.         long                            menuChoice
  59.     );
  60. void                                AdjustMenus(void);
  61. void                                AdjustEditMenu(
  62.         Boolean                            isDeskAcc
  63.     );
  64. void                                DeleteAllWindows(void);
  65.  
  66. void
  67. main(void)
  68. {
  69.         InitMacintosh();
  70.         gDebugOnError = (Get1Resource('CODE', 0) == NULL);    /* Think C hack        */
  71.         TRY {
  72.             InitApplication();
  73.              MakeDocumentWindow();
  74.             while (gQuitNow == FALSE) {
  75.                 if (gUpdateMenusNeeded) {
  76.                     gUpdateMenusNeeded = FALSE;
  77.                     AdjustMenus();
  78.                 }
  79.                 EventLoop();
  80.                 ProcessAudit();
  81.                 if (gParameterUpdateNeeded)
  82.                     WriteAuditParameters();
  83.             }
  84.         }
  85.         CATCH {
  86.             ErrorAlert(STATUS, MESSAGE, TRUE);    /* Fatal                        */
  87.             NO_PROPAGATE;                        /* Nothing to propagate to!        */
  88.         }
  89.         ENDTRY;
  90.         DeleteAllWindows();
  91.         if (gParameterFileRefNum != kNoParamFile) {
  92.             CloseResFile(gParameterFileRefNum);
  93.             gParameterFileRefNum = kNoParamFile;
  94.         }
  95.         ExitToShell();
  96. }
  97.  
  98. /*
  99.  * EventLoop runs the "main" event loop. It calls subroutines for anything
  100.  * of particular interest, such as mouse-clicks.
  101.  */
  102. void
  103. EventLoop(void)
  104. {
  105.         long                        menuChoice;
  106.         register WindowPtr            theWindow;
  107.         GrafPtr                        savePort;
  108.         Boolean                        isActivating;
  109.         
  110.         WaitNextEvent(
  111.             everyEvent,
  112.             &EVENT,
  113.             (gInForeground) ? kForegroundTicks : kBackgroundTicks,
  114.             NULL
  115.         );
  116.         theWindow = FrontWindow();
  117.         switch (EVENT.what) {
  118.         case nullEvent:
  119.             break;
  120.         case keyDown:
  121.         case autoKey:
  122.             if ((EVENT.message & charCodeMask) == '.'
  123.              && (EVENT.modifiers & cmdKey) != 0) {
  124.                 FlushEvents(keyDown | autoKey, 0);
  125.                 gQuitNow = TRUE;
  126.             }
  127.             else if ((EVENT.modifiers & cmdKey) != 0) {
  128.                 if (EVENT.what == keyDown) {
  129.                     menuChoice = MenuKey(EVENT.message & charCodeMask);
  130.                     if (HiWord(menuChoice) != 0 && IsOurWindow(theWindow))
  131.                         DoCommand(theWindow, menuChoice);
  132.                     else if (IsOurWindow(theWindow))
  133.                         DoWindowKeyDown((DocumentPtr) theWindow);
  134.                     else {
  135.                         SysBeep(10);
  136.                     }
  137.                 }
  138.             }
  139.             else if (IsOurWindow(theWindow))
  140.                 DoWindowKeyDown((DocumentPtr) theWindow);
  141.             else {
  142.                 SysBeep(10);
  143.             }
  144.             break;
  145.         case mouseDown:
  146.             DoMouseEvent();
  147.             break;
  148.         case updateEvt:
  149.             theWindow = (WindowPtr) EVENT.message;
  150.             GetPort(&savePort);
  151.             SetPort(theWindow);
  152.             BeginUpdate(theWindow);
  153.             EraseRect(&theWindow->portRect);
  154.             UpdateControls(theWindow, theWindow->visRgn);
  155.             if (IsOurWindow(theWindow))
  156.                 UpdateDocumentWindow((DocumentPtr) theWindow);
  157.             EndUpdate(theWindow);
  158.             SetPort(savePort);
  159.             break;
  160.         case activateEvt:
  161.             theWindow = (WindowPtr) EVENT.message;
  162.             isActivating = ((EVENT. modifiers & activeFlag) != 0);
  163.             goto activateEvent;
  164.             break;
  165.         case osEvt:
  166.             switch (((unsigned long) EVENT.message) >> 24) {
  167.             case mouseMovedMessage:
  168.                 break;
  169.             case suspendResumeMessage:
  170.                 isActivating = ((EVENT.message & 0x01) != 0);
  171. activateEvent:    if (isActivating) {
  172.                     /*
  173.                      * Activate this window. Note that activate events
  174.                      * define theWindow from the EventMessage, while
  175.                      * suspend/resume events use the pre-set FrontWindow.
  176.                      */
  177.                     SelectWindow(theWindow);
  178.                     SetPort(theWindow);
  179.                     (void) TEFromScrap();        /* Not really needed    */
  180.                     if (IsOurWindow(theWindow)) {
  181.                         ActivateDocumentWindow(
  182.                             (DocumentPtr) theWindow,
  183.                             isActivating
  184.                         );
  185.                     }
  186.                     else {
  187.                         /*
  188.                          * Hmm, can it be a desk accessory window?
  189.                          */
  190.                     }
  191.                     gInForeground = isActivating;
  192.                     gUpdateMenusNeeded = TRUE;
  193.                 }
  194.                 break;                        /* Break from osEvt switch    */
  195.             }
  196.             break;                            /* Break from event switch    */
  197.         }
  198. }
  199.  
  200. /*
  201.  * Look at all of the windows. If any are ours, drain its audit record.
  202.  */
  203. void
  204. ProcessAudit(void)
  205. {
  206.         register WindowPtr                thisWindow;
  207.         register WindowPeek                nextWindow;
  208.         
  209.         thisWindow = FrontWindow();
  210.         while (thisWindow != NULL) {
  211.             nextWindow = ((WindowPeek) thisWindow)->nextWindow;
  212.             if (IsOurWindow(thisWindow))
  213.                 ProcessAuditDocument((DocumentPtr) thisWindow);
  214.             thisWindow = (WindowPtr) nextWindow;
  215.         }
  216. }
  217.  
  218.  
  219. /*
  220.  * DoMouseEvent: the user clicked on something. Handle application-wide
  221.  * processing here, or call a DisplayAudit function for specific action.
  222.  */
  223. void
  224. DoMouseEvent(void)
  225. {
  226.         WindowPtr                    theWindow;
  227.         short                        whichPart;
  228.         
  229.         whichPart = FindWindow(EVENT.where, &theWindow);
  230.         if (theWindow == NULL)
  231.             theWindow = FrontWindow();
  232.         if (whichPart == inMenuBar && IsOurWindow(theWindow) == FALSE)
  233.             theWindow = FrontWindow();
  234.         switch (whichPart) {
  235.         case inDesk:
  236.             break;
  237.         case inMenuBar:
  238.             InitCursor();
  239.             DoCommand(theWindow, MenuSelect(EVENT.where));
  240.             break;
  241.         case inDrag:
  242.             DragWindow(theWindow, EVENT.where, &qd.screenBits.bounds);
  243.             break;
  244.         case inGoAway:
  245.             if (TrackGoAway(theWindow, EVENT.where)) {
  246.                 if (IsOurWindow(theWindow)) {
  247.                     DisposeDocumentWindow((DocumentPtr) theWindow);
  248.                     if (gOpenWindowCount <= 0)
  249.                         gQuitNow = TRUE;
  250.                 }
  251.                 else {
  252.                     SysBeep(10);
  253.                 }
  254.             }
  255.             break;
  256.         case inZoomIn:
  257.         case inZoomOut:
  258.             if (IsOurWindow(theWindow)
  259.              && TrackBox(theWindow, EVENT.where, whichPart)) {
  260.                  DoZoomWindow(theWindow, whichPart);
  261.                  goto resizeWindow;
  262.             }
  263.             break;
  264.         case inGrow:
  265.             if (IsOurWindow(theWindow)
  266.              && DoGrowWindow(theWindow, kMinWindowWidth, kMinWindowHeight)) {
  267. resizeWindow:    DecorateWindow((DocumentPtr) theWindow);
  268.                 gParameterUpdateNeeded = TRUE;
  269.             }
  270.             break;
  271.         case inContent:
  272.             if (theWindow != FrontWindow()) {
  273.                 SelectWindow(theWindow);
  274.                 SetPort(theWindow);
  275.             }
  276.             else if (IsOurWindow(theWindow))
  277.                 DoContentClick((DocumentPtr) theWindow);
  278.             else {
  279.                 /* Nothing happens here */
  280.             }
  281.             break;
  282.         default:
  283.             break;
  284.         }
  285. }
  286.  
  287. /*
  288.  * A menu choice.
  289.  */
  290. void
  291. DoCommand(
  292.         WindowPtr                        theWindow,
  293.         long                            menuChoice
  294.     )
  295. {
  296.         short                            menuItem;
  297.         Str255                            menuText;
  298.         GrafPtr                            savePort;
  299.         
  300.         menuItem = LoWord(menuChoice);
  301.         switch (HiWord(menuChoice)) {
  302.         case MENU_Apple:
  303.             if (menuItem != kAppleAbout) {
  304.                 GetItem(gAppleMenu, menuItem, menuText);
  305.                 AdjustEditMenu(TRUE);
  306.                 GetPort(&savePort);
  307.                 OpenDeskAcc(menuText);
  308.                 SetPort(savePort);
  309.                 AdjustEditMenu(IsOurWindow(theWindow) == FALSE);
  310.             }
  311.             break;
  312.         case MENU_File:
  313.             switch (menuItem) {
  314.             case kFileNewWindow:
  315.                 MakeDocumentWindow();
  316.                 break;
  317.             case kFileCloseWindow:
  318.                 if (IsOurWindow(theWindow)) {
  319.                     DisposeDocumentWindow((DocumentPtr) theWindow);
  320.                     if (gOpenWindowCount <= 0)
  321.                         gQuitNow = TRUE;
  322.                 }
  323.                 break;
  324.             case kFileSaveAs:
  325.                 if (IsOurWindow(theWindow))
  326.                     DoDocumentSaveAs((DocumentPtr) theWindow);
  327.                 break;
  328.             case kFileCloseFile:
  329.                 if (IsOurWindow(theWindow))
  330.                     DoDocumentCloseFile((DocumentPtr) theWindow);
  331.                 break;
  332.             case kFileQuit:
  333.                 gQuitNow = TRUE;
  334.                 break;
  335.             default:
  336.                 SysBeep(10);
  337.                 break;
  338.             }
  339.             break;
  340.         case MENU_Edit:
  341.             if (SystemEdit(menuItem - 1) == FALSE)
  342.                 SysBeep(10);
  343.             break;
  344.         }
  345.         HiliteMenu(0);
  346.         gUpdateMenusNeeded = TRUE;
  347. }        
  348.  
  349. void
  350. AdjustMenus(void)
  351. {
  352.         register WindowPtr                theWindow;
  353.         register DocumentPtr            documentPtr;
  354.         
  355.         theWindow = FrontWindow();
  356.         EnableItem(gFileMenu, kFileQuit);
  357.         EnableItem(gFileMenu, kFileNewWindow);
  358.         if (IsOurWindow(theWindow)) {
  359.             EnableItem(gFileMenu, kFileCloseWindow);
  360.             documentPtr = (DocumentPtr) theWindow;
  361.             if (DOC.logFileRefNum == 0) {
  362.                 EnableItem(gFileMenu, kFileSaveAs);
  363.                 DisableItem(gFileMenu, kFileCloseFile);
  364.             }
  365.             else {
  366.                 EnableItem(gFileMenu, kFileCloseFile);
  367.                 DisableItem(gFileMenu, kFileSaveAs);
  368.             }
  369.             CheckItem(gFileMenu, kFileSaveAs, DOC.logFileRefNum != 0);
  370.         }
  371.         AdjustEditMenu(IsOurWindow(theWindow) == FALSE);
  372. }
  373.  
  374. void
  375. AdjustEditMenu(
  376.         Boolean                            isDeskAcc
  377.     )
  378. {
  379.         if (isDeskAcc) {
  380.             EnableItem(gEditMenu, kEditUndo);
  381.             EnableItem(gEditMenu, kEditCut);
  382.             EnableItem(gEditMenu, kEditCopy);
  383.             EnableItem(gEditMenu, kEditPaste);
  384.             EnableItem(gEditMenu, kEditClear);
  385.         }
  386.         else {
  387.             DisableItem(gEditMenu, kEditUndo);
  388.             DisableItem(gEditMenu, kEditCut);
  389.             DisableItem(gEditMenu, kEditCopy);
  390.             DisableItem(gEditMenu, kEditPaste);
  391.             DisableItem(gEditMenu, kEditClear);
  392.         }
  393. }
  394.  
  395. void
  396. DeleteAllWindows(void)
  397. {
  398.         WindowPtr                        thisWindow;
  399.         WindowPeek                        nextWindow;
  400.         
  401.         thisWindow = FrontWindow();
  402.         while (thisWindow != NULL) {
  403.             nextWindow = ((WindowPeek) thisWindow)->nextWindow;
  404.             if (IsOurWindow(thisWindow))
  405.                 DisposeDocumentWindow((DocumentPtr) thisWindow);
  406.             thisWindow = (WindowPtr) nextWindow;
  407.         }
  408. }
  409.  
  410. void
  411. InitMacintosh(void)
  412. {
  413.         int                                i;
  414.         long                            response;
  415.         
  416.         MaxApplZone();
  417.         InitGraf(&qd.thePort);
  418.         InitFonts();
  419.         InitWindows();
  420.         InitMenus();
  421.         TEInit();
  422.         InitDialogs(NULL);
  423.         HNoPurge((Handle) GetCursor(watchCursor));
  424.         SetCursor(*GetCursor(watchCursor));
  425.         for (i = 0; i < 3; i++)
  426.             EventAvail(everyEvent, &EVENT);
  427.         if (Gestalt(gestaltQuickdrawVersion, &response) == noErr
  428.          && response >= gestalt8BitQD)
  429.              gHasColorQuickDraw = TRUE;
  430.  
  431. }
  432.  
  433. void
  434. InitApplication(void)
  435. {
  436.         Handle                            menuBarHdl;
  437.         
  438.         menuBarHdl = GetNewMBar(MBAR_MenuBar);
  439.         FailNIL(menuBarHdl, kErrInitialization);
  440.         SetMenuBar(menuBarHdl);
  441.         gAppleMenu = GetMHandle(MENU_Apple);
  442.         FailNIL(gAppleMenu, kErrInitialization);
  443.         gFileMenu = GetMHandle(MENU_File);
  444.         FailNIL(gFileMenu, kErrInitialization);
  445.         gEditMenu = GetMHandle(MENU_Edit);
  446.         FailNIL(gEditMenu, kErrInitialization);
  447.         AddResMenu(GetMHandle(MENU_Apple), 'DRVR');
  448.         DrawMenuBar();
  449.         gFontMenu = GetMenu(MENU_Font);
  450.         FailNIL(gFontMenu, kErrInitialization);
  451.         AddResMenu(gFontMenu, 'FONT');
  452.         gFontSizeMenu = GetMenu(MENU_FontSize);
  453.         FailNIL(gFontSizeMenu, kErrInitialization);
  454.         OpenAuditParameterFile();
  455.         ReadAuditParameters();
  456.         InitCursor();
  457. }
  458.  
  459. /*
  460.  * This displays an error message and its accompaning text. It should be
  461.  * redone to use a message index (to an STR# resource.
  462.  */
  463. void    
  464. ErrorAlert(
  465.         OSErr                            status,
  466.         short                            messageIndex,
  467.         Boolean                            fatal
  468.     )
  469. {
  470.         Str15                            statusText;
  471.         short                            alertID;
  472.         Str255                            errorMsg;
  473.         
  474.         NumToString(status, statusText);
  475.         GetIndString(errorMsg, STRN_Messages, messageIndex);
  476.         alertID = (fatal) ? ALRT_Fatal : ALRT_NonFatal;
  477.         ParamText(statusText, errorMsg, "\p", "\p");
  478.         StopAlert(alertID, NULL);
  479. }
  480.  
  481.